import Vue from 'vue'
const user = {
	state: {
		userinfo: {},
		isAdmin: false, //是否是超级管理员或机构管理员
		isSchoolMaster: false, //是否是校长
		isMangAdmin: false, //是否是运营账户
		orgIsWired: 0, //机构的设备类型
		teacherInfo: null //老师信息
	},

	mutations: {
		SET_USER_INFO: (state, info) => {
			state.userinfo = info;
			state.orgIsWired = info.isWired || 0
		},
		SET_TEACHER_INFO: (state, teacherInfo) => {
			state.teacherInfo = teacherInfo
		}
	},

	actions: {
		// 获取用户信息
		GetInfo({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				try {
					const result = Vue.ls.get('userInfo');
					if (result) {
						//state.isAdmin = result.username == 'superadmin';
						let roleCodes = result.roleBos.map(item => item.roleCode)
						//如果是机构管理员或者超级管理员

						if (roleCodes.includes('org_admin') || roleCodes.includes('super_admin')) {
							state.isAdmin = true
						} else {
							state.isAdmin = false
						}
						console.log("是否超级管理员或机构管理员" + JSON.stringify(state.isAdmin))
						//是否是校长
						if (roleCodes.includes('school_master')) {
							state.isSchoolMaster = true
						} else {
							state.isSchoolMaster = false
						}
						console.log("是否校长" + JSON.stringify(state.isSchoolMaster))
						//是否是运营账户
						if (roleCodes.includes('mang_admin')) {
							state.isMangAdmin = true
						} else {
							state.isMangAdmin = false
						}
						console.log("是否运营账户" + JSON.stringify(state.isMangAdmin))
						commit('SET_USER_INFO', result)
						resolve(result)
					} else {
						reject()
					}

				} catch (e) {
					reject()
				}
			})
		},


	},
}

export default user